package com.hq.ims.userpc.busservice;

import com.hq.ims.data.service.BaseBusiInfoService;
import com.hq.ims.data.service.BaseCompanyService;
import com.hq.ims.data.vo.statistics.statisticsEnum.EnumDimension;
import com.hq.ims.userpc.vo.UsertatisticsMapstruct;
import com.hq.utils.util.LocalDateTimeUtils;
import com.hq.utils.util.StringUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 经营信息统计
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class UserStatisticsService {
    @Resource
    private BaseBusiInfoService baseBusiInfoService;
    @Resource
    private BaseCompanyService baseCompanyService;

//    /**
//     * 平台端企业发布和接收统计
//     *
//     * @param searchData 时间类型
//     */
//    public UsertatisticsMapstruct.CompanyBusiInfoCountStatisticsVo userCompanyPublishInfoStatistics(
//            UsertatisticsMapstruct.StatisticsSearchData searchData
//    ) {
//        SpringBootValidateUtils.validate(searchData);
//        //获取当前公司及以下的公司
//        List<String> childrenIdList = baseCompanyService.getChildrenIdList(searchData.getOrgId(), true);
//
//        LocalDateTime[] enumDimensionTimeInterval = this.getEnumDimensionTimeInterval(searchData.getDateType());
//        LocalDateTime startDateTime = enumDimensionTimeInterval[0];
//        LocalDateTime endDateTime = enumDimensionTimeInterval[1];
//
//        //发布经营信息统计
//        List<Map> companyStatisticsBusInfoPublishCountByDateTypeDataList = baseBusiInfoService.getBaseMapper().getCompanyStatisticssBusInfoPublishDivideMoneyByDateType(
//                searchData.getDateType().name(), startDateTime, endDateTime, childrenIdList, null
//        );
//        //发布经营信息的公司统计
//        List<Map> companyStatisticsBusInfoPublishCompanyCountByDateTypeList = baseBusiInfoService.getBaseMapper().getCompanyStatisticssBusInfoPublishDivideMoneyByDateType(
//                searchData.getDateType().name(), startDateTime, endDateTime, childrenIdList, null
//        );
//        //被接收数量
//        List<Map> companyStatisticsBeReceiveCountByDateTypeList = baseBusiInfoService.getBaseMapper().getCompanyStatisticsBeReceiveCountByDateType(
//                searchData.getDateType().name(), startDateTime, endDateTime, childrenIdList, null
//        );
//        //被接收公司数量
//        List<Map> companyStatisticssBeReceiveCountCountByDateTypeList = baseBusiInfoService.getBaseMapper().getCompanyStatisticssBeReceiveCompanyCountByDateType(
//                searchData.getDateType().name(), startDateTime, endDateTime, childrenIdList, null
//        );
//
//        //返回对象
//        UsertatisticsMapstruct.CompanyBusiInfoCountStatisticsVo vo = new UsertatisticsMapstruct.CompanyBusiInfoCountStatisticsVo();
//        //预生成日期
//        List<String> dateStrList = this.generateStrDate(searchData.getDateType(), startDateTime, endDateTime);
//
//        //发布经营信息数量和发布的企业数量
//        Map<String, UsertatisticsMapstruct.CompanyBusiInfoPublishCountStatisticsVo> publishBusiInfoMap = new HashMap<>();
//        List<UsertatisticsMapstruct.CompanyBusiInfoPublishCountStatisticsVo> publishBusiInfoCountList = new ArrayList<>();
//        vo.setPublishBusiInfoCountList(publishBusiInfoCountList);
//        for (String dateStr : dateStrList) {
//            UsertatisticsMapstruct.CompanyBusiInfoPublishCountStatisticsVo voObj = new UsertatisticsMapstruct.CompanyBusiInfoPublishCountStatisticsVo();
//            publishBusiInfoCountList.add(voObj);
//            publishBusiInfoMap.put(dateStr, voObj);
//
//            voObj.setDateStr(dateStr);
//            voObj.setPublishBusiInfoCount(0);
//            voObj.setPublishBusiInfoCompanyCount(0);
//        }
//
//
//        for (Map map : companyStatisticsBusInfoPublishCountByDateTypeDataList) {
//            String dateStr = map.get("dateStr").toString();
//            UsertatisticsMapstruct.CompanyBusiInfoPublishCountStatisticsVo voObj = publishBusiInfoMap.get(dateStr);
//            voObj.setPublishBusiInfoCount(Integer.parseInt(map.get("count").toString()));
//        }
//        for (Map map : companyStatisticsBusInfoPublishCompanyCountByDateTypeList) {
//            String dateStr = map.get("dateStr").toString();
//            UsertatisticsMapstruct.CompanyBusiInfoPublishCountStatisticsVo voObj = publishBusiInfoMap.get(dateStr);
//            voObj.setPublishBusiInfoCompanyCount(Integer.parseInt(map.get("count").toString()));
//        }
//
//
//        //经营信息接收数量和接收企业数量
//        Map<String, UsertatisticsMapstruct.CompanyBeReceiveCountStatisticsVo> receiveBusiInfoMap = new HashMap<>();
//        List<UsertatisticsMapstruct.CompanyBeReceiveCountStatisticsVo> receiveBusiInfoCountList = new ArrayList<>();
//        vo.setReceiveBusiInfoCountList(receiveBusiInfoCountList);
//        for (String dateStr : dateStrList) {
//            UsertatisticsMapstruct.CompanyBeReceiveCountStatisticsVo voObj = new UsertatisticsMapstruct.CompanyBeReceiveCountStatisticsVo();
//            receiveBusiInfoCountList.add(voObj);
//            receiveBusiInfoMap.put(dateStr, voObj);
//
//            voObj.setDateStr(dateStr);
//            voObj.setReceiveBusiInfoCount(0);
//            voObj.setReceiveBusiInfoCompanyCount(0);
//        }
//
//        for (Map map : companyStatisticsBeReceiveCountByDateTypeList) {
//            String dateStr = map.get("dateStr").toString();
//            UsertatisticsMapstruct.CompanyBeReceiveCountStatisticsVo voObj = receiveBusiInfoMap.get(dateStr);
//            voObj.setReceiveBusiInfoCount(Integer.parseInt(map.get("count").toString()));
//        }
//        for (Map map : companyStatisticssBeReceiveCountCountByDateTypeList) {
//            String dateStr = map.get("dateStr").toString();
//            UsertatisticsMapstruct.CompanyBeReceiveCountStatisticsVo voObj = receiveBusiInfoMap.get(dateStr);
//            voObj.setReceiveBusiInfoCompanyCount(Integer.parseInt(map.get("count").toString()));
//        }
//        return vo;
//    }

    /**
     * 平台端企业收益统计
     *
     * @param searchData 查询条件
     */
    public UsertatisticsMapstruct.CompanyEarningsMoneyStatisticsVo companyEarningsStatistics(
            UsertatisticsMapstruct.StatisticsSearchData searchData
    ) {
        SpringBootValidateUtils.validate(searchData);

        //默认查所有
        List<String> childrenIdList = new ArrayList<>(0);
        childrenIdList.add(searchData.getOrgId());
//        if (StringUtils.isEmpty(searchData.getOrgId())) {
//            //获取当前公司及以下的公司
//            childrenIdList = baseCompanyService.getChildrenIdList(searchData.getOrgId(), true);
//        }

        LocalDateTime[] enumDimensionTimeInterval = this.getEnumDimensionTimeInterval(searchData.getDateType());
        LocalDateTime startDateTime = enumDimensionTimeInterval[0];
        LocalDateTime endDateTime = enumDimensionTimeInterval[1];

        //获取时间
        //预生成日期
        List<String> dateStrList = this.generateStrDate(searchData.getDateType(), startDateTime, endDateTime);
        //初始化返回对象
        UsertatisticsMapstruct.CompanyEarningsMoneyStatisticsVo companyEarningsMoneyStatisticsVo = new UsertatisticsMapstruct.CompanyEarningsMoneyStatisticsVo();

        //----------------------- 金额钱
        List<Map> companyStatisticssBusInfoPublishDivideMoneyByDateType = baseBusiInfoService.getBaseMapper().getReceiveFeedbackBusInfoMoneyByDateType(
                searchData.getDateType().name(), startDateTime, endDateTime, childrenIdList, null
        );

        List<UsertatisticsMapstruct.CompanyEarningsDateMoneyStatisticsVo> companyEarningsDateMoneyStatisticsVoList = new ArrayList<>();
        companyEarningsMoneyStatisticsVo.setMoneyList(companyEarningsDateMoneyStatisticsVoList);
        Map<String, UsertatisticsMapstruct.CompanyEarningsDateMoneyStatisticsVo> companyEarningsDateMoneyStatisticsVoMap = new HashMap<>();
        for (String dateStr : dateStrList) {
            UsertatisticsMapstruct.CompanyEarningsDateMoneyStatisticsVo voObj = new UsertatisticsMapstruct.CompanyEarningsDateMoneyStatisticsVo();
            companyEarningsDateMoneyStatisticsVoList.add(voObj);
            companyEarningsDateMoneyStatisticsVoMap.put(dateStr, voObj);

            voObj.setDateStr(dateStr);
            voObj.setMoney(BigDecimal.ZERO);
        }

        //补全数据
        for (Map map : companyStatisticssBusInfoPublishDivideMoneyByDateType) {
            String dateStr = map.get("dateStr").toString();
            String moneyStr = map.getOrDefault("money", StringUtils.FH_ZERO).toString();
            UsertatisticsMapstruct.CompanyEarningsDateMoneyStatisticsVo voObj = companyEarningsDateMoneyStatisticsVoMap.get(dateStr);
            voObj.setMoney(new BigDecimal(moneyStr));
        }

        //----------------------- 数量线
        //数量统计
        List<Map> companyStatisticssBusInfoPublishDivideCountByDateType = baseBusiInfoService.getBaseMapper().getReceiveFeedbackBusInfoCountByDateType(
                searchData.getDateType().name(), startDateTime, endDateTime, childrenIdList, null
        );

        List<UsertatisticsMapstruct.CompanyEarningsDateCountStatisticsVo> companyEarningsDateCountStatisticsVoList = new ArrayList<>();
        companyEarningsMoneyStatisticsVo.setCountList(companyEarningsDateCountStatisticsVoList);
        Map<String, UsertatisticsMapstruct.CompanyEarningsDateCountStatisticsVo> companyEarningsDateCountStatisticsVoListMap = new HashMap<>();
        for (String dateStr : dateStrList) {
            UsertatisticsMapstruct.CompanyEarningsDateCountStatisticsVo voObj = new UsertatisticsMapstruct.CompanyEarningsDateCountStatisticsVo();
            companyEarningsDateCountStatisticsVoList.add(voObj);
            companyEarningsDateCountStatisticsVoListMap.put(dateStr, voObj);

            voObj.setDateStr(dateStr);
            voObj.setCount(0);
        }

        //补全数据
        for (Map map : companyStatisticssBusInfoPublishDivideCountByDateType) {
            String dateStr = map.get("dateStr").toString();
            String countStr = map.getOrDefault("count", StringUtils.FH_ZERO).toString();
            UsertatisticsMapstruct.CompanyEarningsDateCountStatisticsVo voObj = companyEarningsDateCountStatisticsVoListMap.get(dateStr);
            voObj.setCount(Integer.parseInt(countStr));
        }

        //如果时间类型选择日,将日期改为MM-dd
        if(searchData.getDateType() == EnumDimension.Day){
            for (UsertatisticsMapstruct.CompanyEarningsDateMoneyStatisticsVo money : companyEarningsMoneyStatisticsVo.getMoneyList()) {
                money.setDateStr(money.getDateStr().substring(5));
            }

            for (UsertatisticsMapstruct.CompanyEarningsDateCountStatisticsVo count : companyEarningsMoneyStatisticsVo.getCountList()) {
                count.setDateStr(count.getDateStr().substring(5));
            }
        }

        return companyEarningsMoneyStatisticsVo;
    }


    /**
     * 将日其进行填充
     *
     * @param dateType 时间类型
     */
    private List<String> generateStrDate(
            EnumDimension dateType,
            LocalDateTime startDateTime,
            LocalDateTime endDateTime
    ) {
        LocalDateTime dynamicStateStartDateTime = startDateTime;
        List<String> dateList = new ArrayList<>();
        while (dynamicStateStartDateTime.isBefore(endDateTime)) {
            switch (dateType) {
                case Day:
                    dateList.add(LocalDateTimeUtils.toStr(dynamicStateStartDateTime, "yyyy-MM-dd"));
                    dynamicStateStartDateTime = dynamicStateStartDateTime.plusDays(1);
                    break;
                case Month:
                    dateList.add(LocalDateTimeUtils.toStr(dynamicStateStartDateTime, "yyyy-MM"));
                    dynamicStateStartDateTime = dynamicStateStartDateTime.plusMonths(1);
                    break;
                case Year:
                    dateList.add(String.valueOf(dynamicStateStartDateTime.getYear()));
                    dynamicStateStartDateTime = dynamicStateStartDateTime.plusYears(1);
                    break;
                default:
                    break;
            }
        }
        return dateList;
    }

    /**
     * 根据时间获取时间范围
     *
     * @param dateType 时间类型
     */
    private LocalDateTime[] getEnumDimensionTimeInterval(EnumDimension dateType) {
        LocalDateTime curDateTime = LocalDateTime.now();
        LocalDateTime[] timeIntervalTimeArr = new LocalDateTime[2];
        LocalDateTime startDateTime = curDateTime;
        LocalDateTime endDateTime = curDateTime;

        switch (dateType) {
            case Day:
                startDateTime = LocalDateTimeUtils.getDateMonthFirstDay(curDateTime);
                endDateTime = LocalDateTimeUtils.getDateMonthLastDay(curDateTime);
                break;
            case Month:
                startDateTime = LocalDateTimeUtils.getDateYearFirstDay(startDateTime);
                endDateTime = LocalDateTimeUtils.getDateYearLastDay(endDateTime);
                break;
            case Year:
                startDateTime = LocalDateTimeUtils.getDateYearFirstDay(startDateTime);
                int year = curDateTime.getYear() - 2024;
                startDateTime = startDateTime.plusYears(year);
                endDateTime = LocalDateTimeUtils.getDateYearLastDay(endDateTime);
                break;
            default:
                break;
        }
        timeIntervalTimeArr[0] = startDateTime;
        timeIntervalTimeArr[1] = endDateTime;
        return timeIntervalTimeArr;
    }


}
